package com.liumuxian.video.controller;

import com.liumuxian.video.pojo.Templateset;
import com.liumuxian.video.service.TemplatesetService;
import entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import java.util.*;

@RestController
@CrossOrigin
public class TemplatesetController {
    @Autowired
    private TemplatesetService templatesetService;

    /**
     * 添加
     * @param templateset
     * @return
     */
    @RequestMapping("templatesetAdd")
    public Result save(Templateset templateset){
        templateset.setPid("104");
        templateset.setName("SoftTest1");
        templateset.setType("0");
        templateset.setVisits(0);
        templateset.setIstop("0");
        templateset.setState(0);
        templateset.setWeight(4);
        templateset.setCreatdata(new Date());
        templateset.setUpdatedate(new Date("2010/02/08"));
        templatesetService.save(templateset);
        return new Result(true, StatusCode.OK,"保存成功",templateset);
    }

    /**
     * 删除
     * @param tid
     * @return
     */
    @RequestMapping("templatesetDelete/{tid}")
    public Result delete(@PathVariable String tid){
        Templateset templateset=templatesetService.getOne(tid);
        templateset.setState(0);
        templatesetService.update(templateset);
        return new Result(true, StatusCode.OK,  "删除成功",templateset);
    }

    /**
     *修改
     * @param templateset
     * @return
     */
    @RequestMapping("templatesetUpdate")
    public Result update(Templateset templateset){
        templateset.setId("1206894550837235712");
        templateset.setPid("101");
        templateset.setName("Accountant");
        templateset.setType("0");
        templateset.setVisits(0);
        templateset.setIstop("0");
        templateset.setState(0);
        templateset.setCreatdata(new Date());
        templateset.setUpdatedate(new Date("2020/10/23"));
        templatesetService.update(templateset);
        return new Result(true, StatusCode.OK,"修改成功",templateset);
    }

    /**
     * 根据id查询
     * @param tid
     * @return
     */
    @RequestMapping("templatesetFindById/{tid}")
    public Result findById(@PathVariable String tid){
        Templateset t=templatesetService.getOne(tid);
        return new Result(true, StatusCode.OK,  "根据id查询成功",t);
    }

    /**
     * 查询全部
     * @return
     */
    @RequestMapping("templatesetFindAll")
    public Result findAll(){
        List<Templateset> list=templatesetService.findAllByState(1);
        for (Templateset t:list) {
            System.out.println(t.toString());
        }
        return new Result(true, StatusCode.OK,"查询成功",list);
    }

    /**
     *分页查询
     * @return
     */
    @RequestMapping("pageTemplateset/{page}/{size}")
    public Result pageTemplateset(@PathVariable Integer page,@PathVariable Integer size){
        Page<Templateset> p=templatesetService.pageTemplateset(page,size);
        return new Result(true, StatusCode.OK,"分页查询成功",p);
    }
}
